﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace TypingG
{
    class tBoxStyle1:tBox
    {
        private Timer timer1 = new Timer();
        private int lenStr;
        private Label lb = new Label();
        public tBoxStyle1()
        {
        }
        public override void getStyle()
        {
            lbFun();
            lenStr = 3;
            
            LengthTyping(lenStr);
            
            this.KeyPress += new KeyPressEventHandler(tBoxStyle1_KeyPress);
            timer1 = new Timer();
            timer1.Interval = StringRandom.Speed / 50;
            timer1.Start();
            timer1.Tick += new EventHandler(timer1_Tick);
            
            

        }

        
        public void LengthTyping(int numlength)
        {
            
            string m=StringRandom.getStRandom(numlength);
            LabelCollection(m);
            int distance=70;
            for (int i = 0; i < this.arrLabel.Count; i++)
            {
                ((Label)this.arrLabel[i]).Size = new System.Drawing.Size(70, 70);
                ((Label)this.arrLabel[i]).Font = new System.Drawing.Font("Times New Roman", 40F, System.Drawing.FontStyle.Bold);
                ((Label)this.arrLabel[i]).Location = new System.Drawing.Point(130+distance*i, this.Height / 2);
                this.Controls.Add(((Label)this.arrLabel[i]));
            }
            processbar();
            
        }
        private int _index = 0;
        private int increaseLv = 0;
        void tBoxStyle1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Convert.ToString(e.KeyChar) == ((Label)this.arrLabel[_index]).Text)
            {

                
                ((Label)this.arrLabel[_index]).ForeColor = System.Drawing.Color.Red;
                if (_index == (lenStr-1))
                {
                   
                        for (int j = 0; j < lenStr; j++) ((Label)this.arrLabel[j]).Dispose();//delete cac key label
                        increaseLv++;
                        if (increaseLv == 1 && lenStr == 10)
                        {
                            this.Enabled = false;
                            timer1.Stop();
                            AccInfo.increaseEXP(AccInfo.WILLHAVEEXP);
                            MessageBox.Show("YouWin.You Have "+Convert.ToString(AccInfo.WILLHAVEEXP) +" EXP");
                            this.Dispose();
                        }
                        if (increaseLv == 2)
                        {
                            this.lenStr++;
                            increaseLv = 0;
                        }

                        this.Clear();
                        _index = -1; //ga'n = -1 vi` de khi _index++ =0(xem *) thi` _index=0 luc' do' thuc hien event keypress
                        LengthTyping(lenStr); //create cac key label moi'

                    
                    
                }
                _index++;  //(*)
            }
            else
            {
               
               
                for (int m = 0; m <= _index; m++) ((Label)this.arrLabel[m]).ForeColor = System.Drawing.Color.White;
             
                this.Clear();
                _index = 0;
            }

            
                   
        }

        void timer1_Tick(object sender, EventArgs e)
        {
            this.lb.Text = "Level " + Convert.ToString(this.lenStr) ;
            if (this.lenStr == 10) this.lb.Text = "FINISH";
            this.Focus();
            this.pBar.Increment(1);
            if (this.pBar.Value == this.pBar.Maximum)
            {
                timer1.Stop();
                MessageBox.Show("YouLose.You Have 0 EXP");
                this.Dispose();
            }
        }
        private void lbFun()
        {
            this.lb.Location = new System.Drawing.Point(this.Width/2-150, this.Height / 2-100);
            this.lb.Size = new System.Drawing.Size(300, 70);
            this.lb.Font = new System.Drawing.Font("Times New Roman", 40F, System.Drawing.FontStyle.Bold);
            
            this.Controls.Add(lb);
        }

    }
}
